package com.szxy.lockdemo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: zwz
 * @Date:2020/10/23 18:01
 * @Description: 锁测试
 **/
public class LockTest {

    // 可重入锁
    private ComReentrantLockDemo lock = new ComReentrantLockDemo();
    // 不可重入锁
//  private NoReentrantLockDemo lock = new NoReentrantLockDemo();

    public void print() throws InterruptedException {
        lock.lock();
        add();
        lock.unlock();
    }

    public void add() throws InterruptedException {
        lock.lock();
        System.out.println(Thread.currentThread().getName() + ": reentrantlock");
        lock.unlock();
    }

    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10; i++) {
            executor.submit(() -> {
                try {
                    new LockTest().print();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }

}
